package com.zhiyin.game.Service;

import com.alibaba.fastjson.JSONObject;
import com.zhiyin.game.bean.Cell;
import com.zhiyin.game.bean.Vector2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class WalkingService {

    @Autowired
    private GameService gameService;

    public List<Vector2> startWalking(int width, int height, int bankSize, String role, Map<String, Object> gameInfo) {
        Random random = new Random();

        //----后续从redis中获取当局游戏的地图数据,暂时本地生成
//        Map gameInfo = gameService.initData(width, height, bankSize);
        //--后续从redis中获取,步数信息
//        int stepCount = 30;
        int stepCount = 3;

        //初始化visited
        Cell[][] cells = (Cell[][]) gameInfo.get("mapInfo");
        for (int i = 0; i < height; i++) {
            Cell[] column = cells[i];
            for (int j = 0; j < width; j++) {
                Cell cell = column[j];
                cell.setVisited(false);
            }
        }

        System.out.println(JSONObject.toJSONString(gameInfo));

        Vector2 roleInfo = (Vector2) gameInfo.get(role);
        int posX = roleInfo.getX();
        int posY = roleInfo.getY();

        Stack<Vector2> stack = new Stack();
        stack.push(new Vector2(posX, posY));

        boolean isback = false;
        Vector2 bakPos = new Vector2(0, 0);

        while (stack.size() <= stepCount) {
            List<String> moveDir = new ArrayList<>();
            Cell cell = getCell(cells, posY, posX);
            cell.setVisited(true);

            if (posY > 0 && !getCell(cells, posY - 1, posX).isVisited() && !cell.isLeftWall())
                moveDir.add("L");
            if (posY < height - 1 && !getCell(cells, posY + 1, posX).isVisited() && !cell.isRightWall())
                moveDir.add("R");
            if (posX < height - 1 && !getCell(cells, posY, posX + 1).isVisited() && !cell.isBottomWall())
                moveDir.add("D");
            if (posX > 0 && !getCell(cells, posY, posX - 1).isVisited() && !cell.isTopWall())
                moveDir.add("U");

            System.out.println(moveDir.toString());
            if (moveDir.size() > 0) {
                String nextMove = moveDir.get(random.nextInt(moveDir.size()));
                System.out.println(nextMove);
                switch (nextMove) {
                    case "L":
                        posY -= 1;
                        break;
                    case "R":
                        posY += 1;
                        break;
                    case "D":
                        posX += 1;
                        break;
                    case "U":
                        posX -= 1;
                        break;
                }
                if (isback) {
                    stack.push(bakPos);
                }
                stack.push(new Vector2(posX, posY));
                isback = false;
                System.out.println("x:" + posX + ";y:" + posY);
            } else {
                System.out.println("死路一条！！" + stack.size());
                if (!stack.empty()) {
                    bakPos = stack.pop();
                    posX = bakPos.getX();
                    posY = bakPos.getY();
                    isback = true;
                    System.out.println("x:" + bakPos.getX() + ";y:" + bakPos.getY());
                } else {
                    System.out.println("stack.empty()");
                    return null;
                }
            }
        }

        System.out.println("栈输出---------------------");
        List<Vector2> movePaths = new ArrayList<>();
        while (!stack.empty()) {
            Vector2 temppos = stack.pop();
            movePaths.add(temppos);
            System.out.println("x:" + temppos.getX() + ";y:" + temppos.getY());
        }

        System.out.println("列表倒叙输出---------------------");
        Collections.reverse(movePaths);

        return movePaths;
    }

    private Cell getCell(Cell[][] map, int y, int x) {
        return map[x][y];
    }

}
